package com.dongge;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Before;
import org.junit.Test;
import org.apache.shiro.mgt.SecurityManager;

/**
 * Created by Administrator on 2016/7/1.
 */
public class Main {

    private Subject subject;
    @Test
    public void testHelloWorld(){
//        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");

        UsernamePasswordToken token = new UsernamePasswordToken("zhang","123");
        try{
            subject.login(token);
            System.out.println("登录成功");
        }catch (AuthenticationException e){
            //登录失败
            System.out.println("登录失败");
        }

        subject.logout();
    }

    @Before
    public void printBefore(){
//        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini");
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        subject = SecurityUtils.getSubject();
        System.out.println(subject);
    }

}
